<template>
  <div class='xtx-goods-page' v-if="goods">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="'/category/' + goods.categories[0].id">{{ goods.categories[0].name }}</XtxBreadItem>
        <XtxBreadItem :to="'/category/sub/' + goods.categories[1].id">{{ goods.categories[1].name }}</XtxBreadItem>
        <XtxBreadItem>{{ goods.name }}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <div class="media">
          <GoodsImage :images="goods.mainPictures"/>
          <GoodsSales/>
        </div>
        <div class="spec">
          <!-- 商品名 -->
          <GoodsName :goods="goods"/>
          <!-- 规格 -->
          <GoodsSku :goods="goods" @change="changeSku"/>
          <!-- 购买量 -->
          <XtxNumBox label="数量" v-model="num" :max="goods.inventory"/>
          <!-- 加入购物车按钮 -->
          <XtxButton type="primary" style="margin-top:20px;">加入购物车</XtxButton>
        </div>
      </div>
      <!-- 商品推荐 -->
      <GoodsRelevant :goodsId="goods.id"/>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <GoodsTabs :goods="goods"/>
          <!-- 注意事项 -->
          <GoodsWarn/>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot :goodsId="goods.id" :type="1"/>
          <GoodsHot :goodsId="goods.id" :type="2"/>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import GoodsRelevant from './components/goods-relevant.vue';
import GoodsName from './components/goods-name.vue';
import GoodsSales from './components/goods-sales.vue';
import GoodsImage from './components/goods-image.vue';
import GoodsSku from './components/goods-sku.vue';
import GoodsTabs from './components/goods-tabs.vue';
import GoodsHot from './components/goods-hot.vue';
import GoodsWarn from './components/goods-warn.vue';
import {findGoods} from '@/api/product';
import {ref, watch, provide, getCurrentInstance} from 'vue';
import {useRoute} from 'vue-router';
import { useCartStore } from "@/store/modules/cart";

export default {
  name: 'GoodsPage',
  components: {GoodsRelevant, GoodsName, GoodsSales, GoodsImage, GoodsSku, GoodsTabs, GoodsHot, GoodsWarn},
  setup() {
    const goods = useGoods()
    const num = ref(1)

    // sku改变时触发
    const changeSku = (sku) => {
      if (sku.skuId) {
        // 价格
        goods.value.price = sku.price
        // 原价
        goods.value.oldPrice = sku.oldPrice
        // 库存
        goods.value.inventory = sku.inventory

        // 添加购物车
        currSku.value = sku
      }
    }

    // 加入购物车
    const currSku = ref(null)
    const useCart = useCartStore()
    // eslint-disable-next-line no-undef
    // 获取组件实例
    const instance = getCurrentInstance()
    const insertCart = async () => {
      if(!currSku.value) {
        return instance.proxy.$message('选择商品规格')
      }
      if(num.value > goods.inventory) {
        return instance.proxy.$message('库存不足')
      }
      useCart.insertCart({
        id:goods.value.id,
        skuId:currSku.value.skuId,
        name:goods.value.name,
        picture:goods.value.mainPicture[0],
        price:currSku.value.price,
        nowPrice:currSku.value.price,
        count:num.value,
        attrsText:currSku.value.specsText,
        selected:true,
        isEffective:true,
        stock:currSku.value.inventory
      }).then(()=> {
        instance.proxy.$message("加入购物车成功", 'success')
      })
    }


    provide('goods', goods)

    return {
      goods,
      num,
      changeSku,
      insertCart
    }
  }
}

const useGoods = () => {
  const goods = ref(null)
  const route = useRoute()
  watch(() => route.params.id, async (newVal) => {
    if (newVal && `/product/${newVal}` === route.path) {
      const {data: {result}} = await findGoods(route.params.id)
      // 让goods为null后用v-if重新渲染，否则goods.value = result会报错
      goods.value = null
      this.$nextTick(() => {
        goods.value = result
      })
    }
  }, {immediate: true})

  return goods
}
</script>

<style scoped lang='less'>
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;

  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }

  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}

.goods-footer {
  display: flex;
  margin-top: 20px;

  .goods-article {
    width: 940px;
    margin-right: 20px;
  }

  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}

.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}</style>